﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract;
using System.ServiceModel;
using KerriganDataContract.Vehicule;

namespace KerriganService.DAC
{
    public static class VehicleDAC
    {
        #region Static Method

        public static VehicleDC GetVehicle(int id)
        {
            VehicleDC dc = new VehicleDC();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    TVehicle entity = new TVehicle();
                    entity = db.TVehicles.Where(t => t.IdVehicle == id).FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return dc;
        }

        /// <summary>
        /// Add and Update method for a Vehicle Object
        /// </summary>
        /// <param name="dc"></param>
        public static void SetVehicle(VehicleDC bo)
        {
            TVehicle entity = new TVehicle();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    if (bo.IdVehicle == 0)
                    {
                        entity = VehicleDAC.CopyBoToEntity(bo);
                        db.AddToTVehicles(entity);
                        db.SaveChanges();
                    }
                    else
                    {
                        entity = db.TVehicles.Where(t => t.IdVehicle == bo.IdVehicle).FirstOrDefault();
                        entity = VehicleDAC.CopyBoToEntity(bo);
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public static void DeleteVehicle(int id)
        {
            TVehicle entity = new TVehicle();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    entity = db.TVehicles.Where(t => t.IdVehicle == id).FirstOrDefault();
                    db.DeleteObject(entity);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public static List<VehicleDC> GetListVehicles()
        {
            List<VehicleDC> list = new List<VehicleDC>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = CopyListEntitiesToListBos(db.TVehicles.ToList());
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        public static List<vwGetVehicle> GetListVehiclesToDisplay()
        {
            List<vwGetVehicle> list = new List<vwGetVehicle>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = db.vwGetVehicles.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        public static List<vwGetVehicleAvailability> GetListVehiclesAvailabilityToDisplay()
        {
            List<vwGetVehicleAvailability> list = new List<vwGetVehicleAvailability>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = db.vwGetVehicleAvailabilities.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        public static List<GetVehicleAvailableByDate_Result> GetVehicleAvailableByDate(DateTime StartDate, DateTime EndDate)
        {
            List<GetVehicleAvailableByDate_Result> list = new List<GetVehicleAvailableByDate_Result>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = db.GetVehicleAvailableByDate(null, null, null, null, StartDate, EndDate, null, null, null).ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        #endregion

        #region Entity/Bo method

        public static VehicleDC CopyEntityToBo(TVehicle entity)
        {
            VehicleDC bo = new VehicleDC();

            try
            {
                bo.IdVehicle = entity.IdVehicle;
                bo.LastRevision = entity.LastRevision;
                bo.Name = entity.Name;
                bo.TypeVehicle = new TypeVehicleDC();
                bo.TypeVehicle = TypeVehicleDAC.CopyEntityToBo(entity.TTypeVehicle);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return bo;
        }

        public static TVehicle CopyBoToEntity(VehicleDC bo)
        {
            TVehicle entity = new TVehicle();

            try
            {
                entity.IdVehicle = bo.IdVehicle;
                entity.LastRevision = bo.LastRevision;
                entity.Name = bo.Name;
                entity.IdTypeVehicleFK = bo.TypeVehicle.IdTypeVehicle;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return entity;
        }

        public static List<VehicleDC> CopyListEntitiesToListBos(List<TVehicle> entities)
        {
            List<VehicleDC> bo = new List<VehicleDC>();

            try
            {
                foreach (TVehicle entity in entities)
                {
                    bo.Add(CopyEntityToBo(entity));
                }
                return bo;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion
    }
}